<template>
  <div v-loading="divLoading" class="clearfix" id="login_wrap">
    <h2 class="title">吴迪教育后台管理系统</h2>
    <div id="login">
      <div class="login--account">
        <span>账号：</span>
        <input type="text" placeholder="请输入账号" name="account" v-model.trim="account" />
      </div>
      <div class="login--password">
        <span>密码：</span>
        <input type="password" placeholder="请输入密码" name="password" v-model.trim="password" @keyup.enter="login" />
      </div>
      <p class="login--btn">
        <button class="theme-all" :loading="loginLoading" id="loginBtn" @click="login">欢迎登录</button>
      </p>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'
import { loginApi } from '@/api/login'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { ElMessage } from 'element-plus'
export default defineComponent({
  setup() {
    let account = ref('')
    let password = ref('')
    let router = useRouter()
    let store = useStore()
    const loginLoading = ref(false)
    const divLoading = ref(false)

    const login = async () => {
      while (!account.value || !password.value) return ElMessage.error('账号或者密码为空')
      const data = {
        account: account.value,
        password: password.value,
      }
      loginLoading.value = true
      divLoading.value = true
      loginApi(data).then((res: any) => {
        if (res.status === 200) {
          ElMessage.success(res.message)
          //重新触发路由
          store.commit('SET_PERMISSION', null)
          store.commit('setUserInfo', res.data)
          router.push({ path: '/' })
          window.localStorage.setItem('token', res.token)
          window.sessionStorage.setItem('token', res.token)
          window.sessionStorage.setItem('userInfo', JSON.stringify(res.data))
        } else {
          ElMessage.error(res.message)
        }
      }).finally(() => {
        loginLoading.value = false
        divLoading.value = false
      })
    }
    return {
      login,
      account,
      password,
      loginLoading,
      divLoading
    }
  },
})
</script>

<style scoped lang="scss">
.title {
  text-align: center;
  font-size: 26px;
  font-weight: 700;
  padding-top: 100px;
}

#login_wrap {
  position: relative;
  height: 100vh;
  background-image: url('../../assets/kebiao.png');
  background-position-x: initial;
  background-position-y: initial;
  background-size: initial;
  background-repeat-x: initial;
  background-repeat-y: initial;
  background-attachment: initial;
  background-origin: initial;
  background-clip: initial;
  background-color: initial;

  >div {
    background: #fff;
    width: 479px;
    height: 325px;
    padding: 30px 40px;
    position: absolute;
    top: 45%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 10px;
    box-shadow: 1px 1px 10px 2px rgb(152, 186, 227);

    >div {
      padding: 10px 0;
      border-bottom: 1px solid #ddd;

      &.login--account {
        margin: 25px 0 30px;
      }

      span {
        color: #666;
        display: inline-block;
        width: 84px;
        font-size: 20px;
      }

      input {
        background: none;
        font-size: 16px;
        border: none;
        height: 30px;
        width: 280px;
        padding-left: 12px;
        box-sizing: border-box;
        color: #666;

        &.error {
          border: 1px solid #f00;
        }

        &::-webkit-input-placeholder {
          color: #aaa;
        }
      }
    }

    p {
      text-align: right;

      &.login--btn {
        button {
          width: 100%;
          height: 50px;
          font-size: 18px;
          background: #0f6171;
          border: none;
          margin-top: 30px;
          color: #fff;
          border-radius: 6px;
          cursor: pointer;
        }
      }

      a {
        color: #fff;
        display: inline-block;
        padding: 0 15px;
        font-size: 14px;
      }
    }
  }
}

#loginBtn {
  background-color: #1890ff !important;
  color: #fff !important;
}
</style>
